<?php
/**
 * @title 139. 单词拆分 - 哈希+递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s
     * @param String[] $wordDict
     * @return Boolean
     */
    function wordBreak($s, $wordDict) {
        /**
         * @since 2021-12-02 长度，字典哈希
         */
        $this->lenArray = $this->wordArray = [];
        foreach ($wordDict as $word){
            $len = strlen($word);
            $this->lenArray[$len] = $len;
            $this->wordArray[$word] = 1;
        }

        /**
         * @since 2021-12-02 排序
         */
        sort($this->lenArray);

        /**
         * @since 2021-12-02 缓存
         */
        $this->dpArray = [];

        /**
         * @since 2021-12-02 递归尝试
         * @return
         */
        $this->s = $s;
        $this->len = strlen($s);
        return self::find(0);
    }

    /**
     * @titlen 是否可以单词拆分
     * @author start2004
     * @since 2021-12-02 Created
     *
     * @param int $i
     * @return boolean
     */
    function find($i = 0){
        /**
         * @since 2021-12-02 检查缓存
         */
        if(isset($this->dpArray[$i])){
            return $this->dpArray[$i];
        } else {}

        /**
         * @since 2021-12-02 最大长度
         */
        $maxlen = $this->len - $i;
        if($maxlen == 0){
            return true;
        } else {}

        /**
         * @since 2021-12-02 长度依次尝试
         */
        foreach ($this->lenArray as $len){
            if($len > $maxlen){
                break;
            } else {}

            /**
             * @since 2021-12-02 从i位置，截取len长度
             * @since 2021-12-02 单词存在，则递归
             */
            $word = substr($this->s, $i, $len);
            // echo "word={$word}", PHP_EOL;
            if(isset($this->wordArray[$word])){
                if(self::find($i+$len) === true){
                    $this->dpArray[$i] = true;
                    return true;
                } else {}
            } else {}
        }

        /**
         * @return
         */
        $this->dpArray[$i] = false;
        return false;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202112/2021.12.02_2.php
 */
$datas = [
    ["leetcode", ["leet", "code"]], // true
    ["applepenapple", ["apple", "pen"]], // true
    ["catsandog", ["cats", "dog", "sand", "and", "cat"]], // false
    ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->wordBreak(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
